import { OrderItem, addOrder, updateOrder } from '@/services/order/order';
import type { ProFormInstance } from '@ant-design/pro-components';
import {
  ModalForm,
  ProFormDigit,
  ProFormSelect,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-components';
import { message } from 'antd';
import { useRef } from 'react';

export type OrderModalProps = {
  visible: boolean;
  onVisibleChange: (visible: boolean) => void;
  initialValues?: Partial<OrderItem>;
  onSuccess: () => void;
};

const OrderModal: React.FC<OrderModalProps> = ({
  visible,
  onVisibleChange,
  initialValues,
  onSuccess,
}) => {
  const formRef = useRef<ProFormInstance>();

  return (
    <ModalForm<Omit<OrderItem, 'id'>>
      formRef={formRef}
      title={initialValues ? '编辑订单' : '新增订单'}
      open={visible}
      onOpenChange={onVisibleChange}
      modalProps={{
        destroyOnClose: true,
        maskClosable: false,
      }}
      initialValues={initialValues}
      preserve={false}
      onFinish={async (values) => {
        try {
          let res;
          if (initialValues?.id) {
            res = await updateOrder({ ...values, id: initialValues.id });
          } else {
            res = await addOrder(values);
          }

          if (res.success) {
            message.success(`${initialValues ? '编辑' : '新增'}成功！`);
            onSuccess();
            return true;
          }
          return false;
        } catch (error) {
          message.error(`${initialValues ? '编辑' : '新增'}失败！`);
          return false;
        }
      }}
    >
      <ProFormText
        name="orderNo"
        label="订单编号"
        placeholder="请输入订单编号"
        rules={[{ required: true, message: '请输入订单编号' }]}
      />
      <ProFormText
        name="customerName"
        label="客户姓名"
        placeholder="请输入客户姓名"
        rules={[{ required: true, message: '请输入客户姓名' }]}
      />
      <ProFormText
        name="productName"
        label="商品名称"
        placeholder="请输入商品名称"
        rules={[{ required: true, message: '请输入商品名称' }]}
      />
      <ProFormDigit
        name="quantity"
        label="数量"
        placeholder="请输入数量"
        min={1}
        rules={[{ required: true, message: '请输入数量' }]}
      />
      <ProFormDigit
        name="totalPrice"
        label="总价"
        placeholder="请输入总价"
        min={0}
        fieldProps={{ precision: 2 }}
        rules={[{ required: true, message: '请输入总价' }]}
      />
      <ProFormSelect
        name="status"
        label="订单状态"
        placeholder="请选择订单状态"
        options={[
          { label: '待处理', value: 'pending' },
          { label: '处理中', value: 'processing' },
          { label: '已完成', value: 'completed' },
          { label: '已取消', value: 'cancelled' },
        ]}
        rules={[{ required: true, message: '请选择订单状态' }]}
      />
      <ProFormTextArea
        name="address"
        label="收货地址"
        placeholder="请输入收货地址"
        rules={[{ required: true, message: '请输入收货地址' }]}
      />
      <ProFormText
        name="phone"
        label="联系电话"
        placeholder="请输入联系电话"
        rules={[{ required: true, message: '请输入联系电话' }]}
      />
    </ModalForm>
  );
};

export default OrderModal; 